<?php
	session_start();

    //error_reporting(0);// turn off errors on release
    $settings = parse_ini_file('troll.ini',true);
    require 'includes/dbconnect.php';
    require 'includes/functions.php';
	
	if(!isset($_SESSION['curr_location'])) $_SESSION['curr_location'] = 'playerselect';
	
    /** only way to here is from town or player select **/ 
    if(
		isset($_SESSION['curr_location']) &&
	   (
		$_SESSION['curr_location'] == 'index' ||
		$_SESSION['curr_location'] == 'playerselect'
		)
	   )
    { 

    	/* check for deleting a character */
    	if(isset($_GET['del']))
    	{
    		/* make sure the id's are set. testing purpose , can delete after release */

    		$sql= "DELETE FROM `characters` WHERE `auid` = ".$_SESSION['id']." AND `id` =".$_GET['del'];
    		
    		try
    		{
    			$db->query($sql);
    			$_SESSION['msg'] = '<p>Character deleted successfully<p>';
    		} 
    		catch(PDOException $e) 
    		{
    			echo $sql.'<br/>';
				echo $e->getMessage().'<br/>';// comment this out on release
				die('Having techical difficulties.Please check back later');

			}
    					
    	} 
    	elseif(isset($_POST['play']))
    	{
			$_SESSION['curr_location'] = 'playerselect';
    		$_SESSION['id'] = $_POST['playwith'];									/* SET ONLINE CHAR ID 				*/
    		setOnlineStatus($_SESSION['id'],1);										/* SET ONLINE STATUS TO 'ONLINE'	*/
    		setLastOnline($_SESSION['id']);											/* SET LAST ONLINE 					*/
    		header('location:stats.php');											/* SEND THEM TO THE STATS SCREEN 1ST*/
    		die();																	/* KILL THIS PAGE 					*/
    	}
	?>
		<html>
		<head>
    		<link rel="stylesheet" type="text/css" href="scripts/troll.css"/>
    		<title>TROLL: Characater Selection</title>
		</head>
		<body>
    		<center>
    		<div class="console">        
        		<table width="800" clas="console">
            		<tr>
                		<td>                
                    		<center>
                    		<img src='images/troll.bmp'/><br/>
									
							<?php
								$sql = "SELECT * FROM characters WHERE auid = ".$_SESSION['id'];
								$query = $db->query($sql);

								$character = getCharacterRecord($_SESSION['id']);
								/* max 5 accounts per character */?>														
								<h1>CHARACTER SELECTION</h1>

								<form method="post" action="playerselect.php"><?php
									echo '<p><table class="plrsel"><th>Character</th><th>Level</th><th>X</th></tr>';
									for($i = 1; $i <= 5; $i++)
									{

										$character = $query->fetch();		
										if ($character != null)
										{
											if($i == 1) 
											{
												$checked = 'checked="checked"'; 
											}
											else
											{ 
												$checked = '';
											}


											echo '<tr>
													<td ><input type="radio" name="playwith" value="'.$character['auid'].'"> '.$character['name'].'</a></td>													
													<td align="center">[<font color="lime">'.$character['level'].'</font>]</td>
													<td align="center">{<font color="red"><a class="delete" href=?del='.$character['id'].'>Delete</a></font>}</td>
									 			</tr>';
									 	}
										else											
											echo '<tr>
														<td><input type="radio"> CREATE</td>
														<td align="center">[ 0 ]</td>
														<td align="center"> { ... }</td>
												</tr>';
									}
									/* save account id */
									echo '</table></p>';
							?>
							
							<p><font color="red">WARNING: character deletion is irreversable!<br/>
							<small>(...and no confirmation!)</small></font></p>
							<p><input type="submit" name="play" value="Play!"/></p>
							<a href="index.php">[ Back ]</a>&nbsp;&nbsp;	
							<a href="create.php">[ Create ]</a>&nbsp;&nbsp;							
							</form>
							
							</center>	
						</td>
					</tr>	
				</table>
			</div>	
			</center>
		</body>	
		</html>	
	<?php
	}
	else
	{	/* they came here from the wrong page */
		header('location:index.php');
	}
